package com.ms.p2p.service.impl;


import com.ms.common.exception.NoticeException;
import com.ms.common.utils.MsDateUtils;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.dao.EvcDao;
import com.ms.p2p.entity.member.Ecv;
import com.ms.p2p.entity.member.Ecv_Type;
import com.ms.p2p.entity.member.Member;
import com.ms.p2p.provider.EvcTypeProvider;
import com.ms.p2p.utils.TradeUtil;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class EvcServiceImpl{
	@Autowired
	EvcTypeServiceImpl evcTypeService;

	@Autowired EvcDao evcDao;


	public List<Map<String, Object>> findPager(String userId, String userName, String begin_date, String end_date, PageBean page) {
		return evcDao.findPager(userId,userName,begin_date,end_date,page.getBegin(),page.getPageSize());
	}
	public int findCount(String userId, String userName, String begin_date, String end_date) {
		return evcDao.findCount(userId,userName,begin_date,end_date);
	}


	public Ecv SelectByid(String evc_id) {
		return evcDao.selectById(evc_id, MsDateUtils.getTime());
	}

	public List SelectByUid(String user_id, Integer time_limit) {
		return evcDao.SelectByUid(user_id,MsDateUtils.getTime(),time_limit);

	}

	public void use(String id, Integer user_id) {

		int i = evcDao.updateEcv(id);

		if (i == 0) {
			throw new NoticeException("红包已使用");
		}
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void awardRegEvc(Member member) {

		List<Ecv_Type> types = evcTypeService.findByType(0 + "");

		for (Ecv_Type type : types) {
			Ecv ecv = new Ecv();

			ecv.setUser_id(member.getId());
			ecv.setUse_count(0);
			ecv.setEcv_type_id(0);
			ecv.setBegin_time(MsDateUtils.getTime());
			ecv.setEnd_time(MsDateUtils.getDayTime(type.getLastday()));
			ecv.setTime_limit(type.getTime_limit());
			ecv.setTender_money_limit(type.getTender_money_limit());
			ecv.setSn(TradeUtil.getTrano() + "");
			ecv.setMoney(type.getMoney());
			ecv.setWay("注册赠送");


		}

	}

	public List selectListByUid(Integer user_id) {
		return	evcDao.selectListByUid(user_id);
	}



}
